﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using JBeamLibrary.Tools;
using JBeamLibrary.VehicleParts;

namespace DriveBeamEditor.Controls.BeamNG.VehiclePartConfig
{
    public partial class VehiclePartInfo : VehiclePartControl
    {
        #region Fields

        #endregion

        #region Properties

        public string SelectedAuthor
        {
            get { return authorsBindingSource.Current as string; }
            set
            {
                listAuthors.SelectedItem = value;
                UpdateUI();
            }
        }

        #endregion

        #region CTORs

        public VehiclePartInfo()
        {
            InitializeComponent();
        }

        #endregion

        #region Events

        private void xbAdd_Click(object sender, EventArgs e)
        {
            /* No author specified */
            if (string.IsNullOrEmpty(tbAuthor.Text))
            {
                MessageBox.Show(
                    this,
                    @"Please provide an author name",
                    @"No author name specified",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Exclamation
                );
                return;
            }

            /* Author exists */
            if (VehiclePart.Authors.Any(author => author.Equals(tbAuthor.Text)))
            {
                MessageBox.Show(
                    this,
                    @"This author has already been added",
                    @"Duplicate value",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Exclamation
                );
                return;
            }

            /* Add new author */
            VehiclePart.AddAuthor(tbAuthor.Text);
            authorsBindingSource.ResetBindings(false);
        }

        private void xbRemove_Click(object sender, EventArgs e)
        {
            /* No author specified */
            if (string.IsNullOrEmpty(tbAuthor.Text))
            {
                MessageBox.Show(
                    this,
                    @"Please select the author to remove",
                    @"No author selected",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Exclamation
                );
                return;
            }

            /* Author does not exist */
            if (!VehiclePart.Authors.Any(author => author.Equals(tbAuthor.Text)))
            {
                MessageBox.Show(
                    this,
                    @"The specified author is absent from the list",
                    @"Author not found",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Exclamation
                );
                return;
            }

            /* Remove author */
            VehiclePart.RemoveAuthor(tbAuthor.Text);
            authorsBindingSource.ResetBindings(false);
        }

        private void listAuthors_SelectedIndexChanged(object sender, EventArgs e)
        {
            UpdateUI();
        }

        #endregion

        #region Methods

        public override void UpdateUI(Control parent = null)
        {
            base.UpdateUI(parent);

            if (parent != null) return;

            tbAuthor.Text = SelectedAuthor.ToStringOrEmpty();
        }

        #endregion
    }
}
